package data

import (
	"encoding/json"
	"github.com/obs-data-process/internal/biz"
	"github.com/obs-data-process/internal/biz/domain"
	processor2 "github.com/obs-data-process/internal/pkg/processor"
	"github.com/pkg/errors"
	"io/ioutil"
)

type ImageRepo struct {
}

func NewImageRepo() biz.ImageRepo {
	return &ImageRepo{}
}

func (*ImageRepo) LoadAll(filename string) ([]*domain.Image, error) {
	bytes, err := ioutil.ReadFile(filename)
	if err != nil {
		return nil, errors.Wrapf(err, "read image data file failed. filename: %s", filename)
	}
	res := make([]*domain.Image, 0)
	err = json.Unmarshal(bytes, &res)
	if err != nil {
		return nil, errors.Wrap(err, "json unmarshal failed")
	}
	p := processor2.NewPrefixProcessor()
	for i, image := range res {
		res[i].Url = p.Process(image.Url)
	}
	return res, err
}
